<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="UTF-8" />
    <meta http-equiv="X-UA-Compatible" content="IE=edge" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <title>2.回调形式的ref</title>
    <!-- 引入react核心库 -->
    <script src="../React-js/react.development.js"></script>
    <!-- 引入react-dom,用于支持react操作DOM -->
    <script
      type="text/javascript"
      src="../React-js/react-dom.development.js"
    ></script>
    <!-- 引入babel,用于将jsx转换为js -->
    <script type="text/javascript" src="../React-js/babel.min.js"></script>

    <style>
      * {
        margin: 0;
        padding: 0;
        box-sizing: border-box;
      }
      body {
        display: flex;
        justify-content: center;
        align-items: center;
        min-height: 100vh;
        background-color: #303030;
      }
      .demo {
        display: flex;
        gap: 20px;
      }
      input {
        border: none;
        outline: none;
        padding: 10px;
        font-size: 18px;
        border-radius: 5px;
      }
      button {
        padding: 10px;
        border-radius: 10px;
        font-size: 18px;
        cursor: pointer;
      }
      button:active {
        transform: scale(0.98);
      }
    </style>
  </head>
  <body>
    <!-- 准备好一个“容器” -->
    <div id="test"></div>
    <script type="text/babel">
      // 1.创建组件
      class MyComponent extends React.Component {
        // 点我提示左边数据
        showData = () => {
          const { inputEl } = this;
          alert(inputEl.value);
        };
        blurData = () => {
          const { inputBlurEl } = this;
          alert(inputBlurEl.value);
        };
        render() {
          // console.log(this);
          return (
            <div className="demo">
              <input
                ref={(currentNode) => (this.inputEl = currentNode)}
                type="text"
                placeholder="点击按钮提示数据"
              />
              <button onClick={this.showData}>点我提示左边数据</button>
              <input
                ref={(currentNode) => (this.inputBlurEl = currentNode)}
                onBlur={this.blurData}
                type="text"
                placeholder="失去焦点提示数据"
              />
            </div>
          );
        }
      }
      // 2.渲染组件到页面
      ReactDOM.render(<MyComponent />, document.getElementById('test'));
    </script>
  </body>
</html>
